package model.objects;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.mxgraph.model.mxCell;
import com.mxgraph.util.mxResources;

public class EntityObject extends ModelingObject {

	private static final long serialVersionUID = -358313430873201797L;

	private boolean selfRelated = false;

	private boolean specialized = false;

	private final int NUMBER_OF_ATTRIBUTES = 2;

	public EntityObject() {
		super("");
	}

	public EntityObject(String name) {
		super(name);
	}

	public void setSelfRelated(boolean selfRelated) {
		this.selfRelated = selfRelated;
	}

	public boolean isSelfRelated() {
		return selfRelated;
	}

	public void setSpecialized(boolean specialized) {
		this.specialized = specialized;
	}

	public boolean isSpecialized() {
		return specialized;
	}

	public int attributesCount() {
		return super.attributesCount() + NUMBER_OF_ATTRIBUTES;
	}

	public void getAttributes(int types[], String names[], String values[], boolean fieldsEnabled[]) {
		super.getAttributes(types, names, values, fieldsEnabled);

		int i = super.attributesCount();

		types[i] = AppConstants.CHECK_BOX;
		names[i] = mxResources.get("selfRelated");
		fieldsEnabled[i] = false;
		values[i++] = selfRelated ? "true" : "false";
		types[i] = AppConstants.CHECK_BOX;
		names[i] = mxResources.get("specialized");
		fieldsEnabled[i] = false;
		values[i] = specialized ? "true" : "false";
	}

	public void setAttributes(String values[]) {
		super.setAttributes(values);

		setSelfRelated(Boolean.parseBoolean(values[2].toString()));
		setSpecialized(Boolean.parseBoolean(values[3].toString()));
	}

	public int windowHeight() {
		return 220;
	}

	public String getToolTip() {
		String tip = "Tipo: Entidade <br>";

		tip += super.getToolTip();
		tip += mxResources.get("selfRelated") + ": ";
		tip += selfRelated ? mxResources.get("yes") : mxResources.get("no");
		tip += "<br>";
		tip += mxResources.get("specialized") + ": ";
		tip += specialized ? mxResources.get("yes") : mxResources.get("no");
		tip += "<br>";

		return tip;
	}

	public void removeReferencesFromParents() {
		Iterator<Object> iterator = getParentObjects().iterator();
		while (iterator.hasNext()) {
			mxCell parent = (mxCell)iterator.next();
			if(parent != null && parent.getValue() instanceof InheritanceObject) {
				InheritanceObject inheritance = (InheritanceObject)parent.getValue();
				if (inheritance.getChildObjects().size() == 2)
					inheritance.setExclusive(false);

				inheritance.removeChildObject(this);
				//				addParentObject(null);
				getParentObjects().clear();
			}
		}
	}

	public String getStyle() {
		return "entity";
	}

	public List<AttributeObject> getPrimaryKeys() {
		List<AttributeObject> primaryKeys = new ArrayList<AttributeObject>();

		Iterator<Object> iterator = getChildObjects().iterator();
		while (iterator.hasNext()) {
			Object object = iterator.next();
			if (((mxCell)object).getValue() instanceof AttributeObject) {
				AttributeObject attribute = (AttributeObject) ((mxCell)object).getValue();
				if (attribute.isIdentifier())
					primaryKeys.add(attribute);
			}
		}
		return primaryKeys;
	}
}
